package org.dbstar.scheduler.actions;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.dbstar.scheduler.beans.CmdJobConf;
import org.dbstar.scheduler.beans.Job;
import org.dbstar.scheduler.beans.OperateType;
import org.dbstar.scheduler.control.JobConfController;
import org.dbstar.scheduler.control.JobController;
import org.dbstar.scheduler.job.JobStatus;


/**
 * 所有Job列表
 * 
 * @author 代波
 * @since 1.0.0
 * @version 1.0.0
 * @date 2012-4-5
 */
public class JobsAction extends ActionSupport {
	private JobController jobController;
	private JobConfController jobConfController;

	private List<Job> jobs;
	private List<Job> readyJobs;
	private List<Job> finishJobs;

	public void setJobController(JobController jobController) {
		this.jobController = jobController;
	}

	public JobConfController getJobConfController() {
		return jobConfController;
	}

	public void setJobConfController(JobConfController jobConfController) {
		this.jobConfController = jobConfController;
	}

	public List<Job> getJobs() {
		return jobs;
	}

	public List<Job> getReadyJobs() {
		return readyJobs;
	}

	public List<Job> getFinishJobs() {
		return finishJobs;
	}

	@Override
	public String execute() throws Exception {
		this.jobs = filterCmdJob(jobController.getAllJobs());
		this.readyJobs = filterCmdJob(jobController.getJobs(JobStatus.ready));
		this.finishJobs = new LinkedList<Job>();
		List<Job> list = filterCmdJob(jobController.getJobs(JobStatus.finish));
		for (int index = Math.max(list.size() - 100, 0), size = list.size(); index < size; index++) {
			finishJobs.add(0, list.get(index));
		}
		operate(OperateType.jobs_list, String.valueOf(jobs.size()));
		return SUCCESS;
	}

	private static List<Job> filterCmdJob(List<Job> list) {
		List<Job> jobs = new ArrayList<Job>();
		for (Job job : list) {
			if (CmdJobConf.JOB_TYPE.equals(job.getType())) {
				jobs.add(job);
			}
		}
		return jobs;
	}
}
